package com.bullsora.kindlerss.controller

import com.bullsora.kindlerss.model.Subscription
import org.springframework.dao.DataIntegrityViolationException
import com.bullsora.kindlerss.model.Feed
import com.bullsora.kindlerss.fulfillment.FulfillmentService

class SubscriptionController {

  FulfillmentService fulfillmentService
  
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def index() {
    redirect(action: "list", params: params)
  }

  def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    def currentUser = getAuthenticatedUser()
    [subscriptionInstanceList: Subscription.findAllByAppUser(currentUser, params), subscriptionInstanceTotal: Subscription.countByAppUser(currentUser)]
  }

  def create() {
    [subscriptionInstance: new Subscription(params), availableFeeds: Feed.findAllByAppUser(getAuthenticatedUser())]
  }

  def save() {
    def subscriptionInstance = new Subscription(params + [appUser: getAuthenticatedUser(), lastChecked : new Date()])
    if (!subscriptionInstance.save(flush: true)) {
      render(view: "create", model: [subscriptionInstance: subscriptionInstance, availableFeeds: Feed.findAllByAppUser(getAuthenticatedUser())])
      return
    }
    fulfillmentService.scheduleFulfillmentTask(subscriptionInstance)
    flash.message = message(code: 'default.created.message', args: [message(code: 'subscription.label', default: 'Subscription'), subscriptionInstance.id])
    redirect(action: "show", id: subscriptionInstance.id)
  }

  def show(Long id) {
    def subscriptionInstance = Subscription.findWhere(id: id, appUser: getAuthenticatedUser())
    if (!subscriptionInstance) {
      flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscription.label', default: 'Subscription'), id])
      redirect(action: "list")
      return
    }

    [subscriptionInstance: subscriptionInstance]
  }

  def edit(Long id) {
    def subscriptionInstance = Subscription.findWhere(id: id, appUser: getAuthenticatedUser())
    if (!subscriptionInstance) {
      flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscription.label', default: 'Subscription'), id])
      redirect(action: "list")
      return
    }

    [subscriptionInstance: subscriptionInstance, availableFeeds: Feed.findAllByAppUser(getAuthenticatedUser())]
  }

  def update(Long id, Long version) {
    def subscriptionInstance = Subscription.findWhere(id: id, appUser: getAuthenticatedUser())
    if (!subscriptionInstance) {
      flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscription.label', default: 'Subscription'), id])
      redirect(action: "list")
      return
    }

    if (version != null) {
      if (subscriptionInstance.version > version) {
        subscriptionInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                                                [message(code: 'subscription.label', default: 'Subscription')] as Object[],
                                                "Another user has updated this Subscription while you were editing")
        render(view: "edit", model: [subscriptionInstance: subscriptionInstance, availableFeeds: Feed.findAllByAppUser(getAuthenticatedUser())])
        return
      }
    }

    subscriptionInstance.properties = params

    if (!subscriptionInstance.save(flush: true)) {
      render(view: "edit", model: [subscriptionInstance: subscriptionInstance])
      return
    }

    fulfillmentService.scheduleFulfillmentTask(subscriptionInstance)
    flash.message = message(code: 'default.updated.message', args: [message(code: 'subscription.label', default: 'Subscription'), subscriptionInstance.id])
    redirect(action: "show", id: subscriptionInstance.id)
  }

  def delete(Long id) {
    def subscriptionInstance = Subscription.findWhere(id: id, appUser: getAuthenticatedUser())
    if (!subscriptionInstance) {
      flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscription.label', default: 'Subscription'), id])
      redirect(action: "list")
      return
    }

    try {
      subscriptionInstance.delete(flush: true)
      fulfillmentService.cancelFulfillmentTask(subscriptionInstance)
      flash.message = message(code: 'default.deleted.message', args: [message(code: 'subscription.label', default: 'Subscription'), id])
      redirect(action: "list")
    }
    catch (DataIntegrityViolationException e) {
      flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'subscription.label', default: 'Subscription'), id])
      redirect(action: "show", id: id)
    }
  }
}
